﻿using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using EvincePayroll.Data;
using EvincePayroll.Logic;
using EvincePayroll.Utility;
using System.Configuration;

namespace Evince.Service
{
    // Start the service and browse to http://<machine_name>:<port>/Service1/help to view the service's generated help page
    // NOTE: By default, a new instance of the service is created for each call; change the InstanceContextMode to Single if you want
    // a single instance of the service to process all calls.	
    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    // NOTE: If the service is renamed, remember to update the global.asax.cs file
    public class LeaveRequestService
    {
        string userEmailId = ConfigurationManager.AppSettings["LoginID"].ToString();

        [WebGet(UriTemplate = "GetLeaveRequestsEmployeeData/{emailId},{userRole}")]
        public List<EmployeeData> GetLeaveRequestsEmployeeData(string emailId, string userRole)
        {
            try
            {
                LeaveRequstLogic leaveLogic = new LeaveRequstLogic(emailId);
                return leaveLogic.GetLeaveRequestsEmployeeData(emailId, userRole);
            }
            catch (Exception ex)
            {
                LogEngine.Log(ex.Message);
                throw;
            }
        }

        [WebInvoke(UriTemplate = "LeaveRequestsEmployee/{emailId}")]
        public List<EmployeeLeaveRequestData> LeaveRequestsEmployee(EmployeeLeaveRequestData employeeLeaveRequestData, string emailId)
        {
            try
            {
                LeaveRequstLogic leaveRequestLogic = new LeaveRequstLogic(emailId);
                return leaveRequestLogic.LeaveRequestsEmployee(employeeLeaveRequestData, emailId);

            }
            catch (Exception ex)
            {
                LogEngine.Log(ex.Message);
                throw;
            }


        }

        [WebInvoke(UriTemplate = "UpdateLeaveRequestStatus/{emailId},{leaveRequestId},{status},{message},{numberOfDays},{flag}")]
        public bool UpdateLeaveRequestStatus(string emailId, string leaveRequestId, string status, string message, string numberOfDays, string flag, DateTime fromDate)
        {
            try
            {
                LeaveRequstLogic leaveLogic = new LeaveRequstLogic(emailId);
                return leaveLogic.UpdateLeaveRequestStatus(leaveRequestId, status, message, numberOfDays, Convert.ToDateTime(fromDate), flag);
            }
            catch (Exception ex)
            {
                LogEngine.Log(ex.Message);
                throw;
            }
        }

        [WebGet(UriTemplate = "LeaveRequestsAdmin/{emailId}")]
        public List<EmployeeLeaveRequestData> LeaveRequestsAdmin(string emailId)
        {
            try
            {
                LeaveRequstLogic leaveLogic = new LeaveRequstLogic(emailId);
                return leaveLogic.LeaveRequestsAdmin();
            }
            catch (Exception ex)
            {
                LogEngine.Log(ex.Message);
                throw;
            }
        }

        [WebGet(UriTemplate = "GetRequestEmployee/{emailId},{status}")]
        public List<EmployeeLeaveRequestData> GetRequestEmployee(string emailId, string status)
        {
            try
            {
                LeaveRequstLogic leaveLogic = new LeaveRequstLogic(emailId);
                return leaveLogic.GetRequestEmployee(emailId, status);
            }
            catch (Exception ex)
            {
                LogEngine.Log(ex.Message);
                throw;
            }

        }

        [WebInvoke(UriTemplate = "AddData/{emailId}")]
        public bool AddData(EmployeeLeaveRequestData employeeLeaveRequest, string emailId)
        {
            try
            {
                LeaveRequstLogic leaveLogic = new LeaveRequstLogic(emailId);
                return leaveLogic.AddData(employeeLeaveRequest, emailId);
            }
            catch (Exception ex)
            {
                LogEngine.Log(ex.Message);
                throw;
            }


        }

        [WebInvoke(UriTemplate = "UpdateData/{Id},{emailId}")]
        public bool UpdateData(EmployeeLeaveRequestData employeeLeaveRequest, string Id, string emailId)
        {
            try
            {
                LeaveRequstLogic leaveLogic = new LeaveRequstLogic(emailId);
                return leaveLogic.UpdateData(employeeLeaveRequest, Guid.Parse(Id), emailId);
            }
            catch (Exception ex)
            {
                LogEngine.Log(ex.Message);
                throw;
            }


        }

        [WebGet(UriTemplate = "Delete/{id},{emailId}")]
        public string DeleteData(string id, string emailId)
        {

            try
            {
                LeaveRequstLogic leaveLogic = new LeaveRequstLogic(emailId);
                return leaveLogic.DeleteData(Convert.ToInt32(id), emailId);
            }
            catch (Exception ex)
            {
                LogEngine.Log(ex.Message);
                throw;
            }


        }


        [WebGet(UriTemplate = "GetLeaveMasterData/{emailId},{roleId}")]
        public LeaveMasterData GetLeaveMasterData(string emailId, string roleId)
        {
            try
            {
                LeaveRequstLogic leaveLogic = new LeaveRequstLogic(emailId);
                return leaveLogic.GetLeaveMasterData(emailId, roleId);
            }
            catch (Exception ex)
            {
                LogEngine.Log(ex.Message);
                throw;
            }

        }

        [WebInvoke(UriTemplate = "GetEmoloyeeLeaveMasterData/")]
        public EmployeeLeaveMasterData GetEmoloyeeLeaveMasterData(EmployeeData employee)
        {
            try
            {
                LeaveRequstLogic leaveLogic = new LeaveRequstLogic(employee.Email);
                return leaveLogic.GetEmoloyeeLeaveMasterData(employee);
            }
            catch (Exception ex)
            {
                LogEngine.Log(ex.Message);
                throw;
            }

        }

        [WebInvoke(UriTemplate = "CreateLeaveMaster/{emailId}")]
        public LeaveMasterData CreateLeaveMaster(LeaveMasterData leaveMaster, string emailId)
        {
            try
            {
                LeaveRequstLogic leaveLogic = new LeaveRequstLogic(emailId);
                return leaveLogic.CreateLeaveMaster(leaveMaster, emailId);
            }
            catch (Exception ex)
            {
                LogEngine.Log(ex.Message);
                throw;
            }

        }

        [WebInvoke(UriTemplate = "CalculateLeaveOfEmployee/{email}")]
        public bool CalculateLeaveOfEmployee(EmployeeLeaveRequestData employeeLeaveRequestData, string email)
        {
            try
            {
                LeaveRequstLogic leaveLogic = new LeaveRequstLogic(email);
                return leaveLogic.CalculateLeaveOfEmployee(employeeLeaveRequestData, true);
            }
            catch (Exception ex)
            {
                LogEngine.Log(ex.Message);
                throw;
            }
        }

        [WebGet(UriTemplate = "CheckLeave/{leaveType},{emailId}")]
        public bool CheckLeave(string leaveType, string emailId)
        {
            try
            {
                LeaveRequstLogic leaveLogic = new LeaveRequstLogic(emailId);
                return leaveLogic.CheckLeave(leaveType, emailId);
            }
            catch (Exception ex)
            {
                LogEngine.Log(ex.Message);
                throw;
            }
        }

        [WebGet(UriTemplate = "LeaveWithdraw/{email},{leaveRequestId},{flag}")]
        public bool LeaveWithdraw(string email, string leaveRequestId, string flag)
        {
            try
            {
                LeaveRequstLogic leaveLogic = new LeaveRequstLogic(email);
                return leaveLogic.LeaveWithdraw(leaveRequestId, Convert.ToBoolean(flag));
            }
            catch (Exception ex)
            {
                LogEngine.Log(ex.Message);
                throw;
            }
        }
    }
}